﻿google.load("jqueryui", "1.8.2");

$(document).ready(function () {
    /*Index Page*/
    $('#Name').click(function () {
        SortData('Name');
    });

    $('#City').click(function () {
        SortData('City');
    });

    /*Index Page*/

    /*Captcha Error*/
    $('#captchaerror').hide();
    /*Captcha Error*/

    /* Create Page */
    $('#preview').click(function () {
        loadLocation();
        return false;
    });
    /* Create Page */

    /*Facebook */

    //load facebook SDK asynchronously
    window.fbAsyncInit = function () {
        FB.init({
            appId: '197268050317385',
            status: true,   // check login status
            cookie: true,   // enable cookies to allow the server to access the session
            xfbml: true     // parse XFBML
        });

        FB.getLoginStatus(function (response) {
            if (response && (response.status !== "unknown")) {
                $('ul.login').hide();
                showWelcome();
            } else {
                $('ul.login').show();
                $('#welcome').hide();
            }
        });

        FB.Event.subscribe('auth.login', function (response) {
            $.post("/UserProfile", { userId: response.session.uid }, function (data, success) {
            });
        });
    };

    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    } ());

    /*Facebook */
});

function logout() {
    FB.logout(function (response) {
        $('#welcome').hide();
        window.location.href="http://www.badmintonbuddy.com/signup";
    });     
}

function showWelcome() {
    FB.api('/me', function (response) {
        $('#welcome').show();
        $('#username').text(response.first_name);
        $('#username').append("<a href=\"#\" onclick=\"javascript:logout();\"> (Not " + response.first_name + "?)</a>");
    });    
}

function showLogin()
{
    FB.login(function (response) {
        if (response.session) {
            $('#fbLogin').hide();
            window.location.href= document.referrer;
        } else {
            // user cancelled login
        }
    });
}

function SortData(param) {
    $.post("/SortData", { sortby: param }, function (data, success) {
        if (data.length > 0) {
            var renderhtml = "";
            $.each(data, function (index, club) {
                if (index % 2 == 0) {
                    renderhtml += "<div class=\"box dark\">"
                }
                else {
                    renderhtml += "<div class=\"box light\">"
                }
                renderhtml += "<h3 class=\"name\">" + club.ClubName + "</h3>";
                renderhtml += "<div class=\"address\">" + club.Address + "<div>" + club.City.CityName + "," + club.City.State.StateName + "-" + club.Zipcode + "</div></div>"
                renderhtml += "<a href=\"Details/" + club.ClubName.replace(/ /g, "-") + "\" class=\"morelink\">more info</a>";
                renderhtml += "</div>";

            });

            $('.boxparent').html(renderhtml);
        }
    });
}

function addNewDetails(formId) {
    if (validateNewClub()) { // first check if clubname and address is entered and then validate captcha
        validateCaptcha(formId);
    }
}

function validateCaptcha(formId) {
    if($('#attempt').val() == $('#solution').val()){
        $('#' + formId + '').submit();                    
    } else{
        $('#captchaerror').show('slow');
    }   
}

function validateNewClub(){
            if (rgbToHex($('#ClubName').css('color')) == '#aaaaaa') {
                $('#ClubName_validationMessage').html("Please enter Club Name").css('color', 'red'); ;
                $('#ClubName_validationMessage').show('slow');
                return false;

            }
            else if (rgbToHex($('#Address').css('color')) == '#aaaaaa') {
                $('#Address_validationMessage').html("Please enter Address for the Club").css('color', 'red'); ;
                $('#Address_validationMessage').show('slow');
                return false;
            }
            $('input:text').each(function () {
                if (rgbToHex($(this).css('color')) == '#aaaaaa') {
                    $(this).val("");
                }
            });

            $('textarea').each(function () {
                if (rgbToHex($(this).css('color')) == '#aaaaaa') {
                    $(this).val("");
                }
            });

            $('#ClubName_validationMessage').hide();
            $('#Address_validationMessage').hide();
            return true;
}


function moreDetails() {
    var image = $('#moreimg').attr('src');
    $('#optional').toggle();
    $('#coll').toggle();
    $('#expnd').toggle();    
}